package windows;

import Base.Dialog;
import thread.Sleep;

import javax.swing.*;
import java.awt.*;

public class MessageDialog extends Dialog {
    private JLabel titleLabel; // 标题
    private JLabel messageLabel; // 正文
    public JButton button = new JButton("确定"); // 确定按钮
    private boolean isAlive = true;   // 是否存活

    public MessageDialog() {
        init();
    }

    public void init() {
        initWindow();
        // 标题
        titleLabel = new JLabel("title");
        titleLabel.setBounds(0, 0, 400, 50);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        titleLabel.setBorder(null);
        window.add(titleLabel);

        // 正文
        messageLabel = new JLabel("message");
        messageLabel.setBounds(0, 50, 400, 100);
        messageLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        messageLabel.setHorizontalAlignment(SwingConstants.CENTER);
        messageLabel.setForeground(new Color(128, 128, 128));
        messageLabel.setBorder(null);
        window.add(messageLabel);

        // 确定按钮
        button.setBackground(Color.white);
        button.setBounds(0, 150, 400, 50);
        button.setFont(new Font("微软雅黑", Font.PLAIN, 24));
        button.addActionListener(e -> close());
        window.add(button);
        setVisible(false);

        // 窗口默认关闭
        window.setVisible(false);
    }

    public JPanel getWindow() {
        return window;
    }

    public void open(String title, String message) {
        isAlive = true;
        if (title == null || title.equals(""))
            title = "title is null";
        if (message == null || message.equals(""))
            message = "message is null";
        System.out.print("弹窗：" + title + " " + message + "\n");
        titleLabel.setText(title);
        messageLabel.setText(message);
        window.setVisible(true);
        setVisible(true);
        // 阻塞进程
//        while (isAlive)
//            Sleep.sleep(10);
    }

    public void open(String message) {
        if (message == null || message.equals(""))
            message = "message is null";
        System.out.print("弹窗：" + message + "\n");
        titleLabel.setText("");
        messageLabel.setText(message);
        messageLabel.setFont(new Font("微软雅黑", Font.PLAIN, 25));
        window.setVisible(true);
        setVisible(true);
    }

    private void close() {
        window.setVisible(false);
        isAlive = false;
        setVisible(false);
    }
}
